import Taro from '@tarojs/taro'
import app from '../../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            topic_id: this.$router.params.id,
            topic: {
                id: '',
                title: '',
                summary: '',
                image: '',
                created_at: '',
            },
            dongtaiss: {
                offset: 0,
                length: 25,
                items: []
            },
        };
        return this.state;
    },
    getTopic() {
        var me = this;
        var data = {topic_id: me.state.topic_id};
        app.get("web/dynamic_topic/topicDetail", data, function(res) {
            var topic = me.state.topic;
            topic = res.data.result;
            me.setState({
                topic: topic
            });
        });
        
    },
    listDongtais() {
        var me = this;
        var data = {
            offset: me.state.dongtaiss.offset, 
            length: me.state.dongtaiss.length,
            topic_id: me.state.topic_id,
        };
        app.get('web/dynamic_topic/dynamicList', data, function(res) {
            var list = res.data.result.list;
            var dongtaiss = me.state.dongtaiss;
            dongtaiss.items = dongtaiss.items.concat(list);
            me.setState({
                total: res.data.result.total,
                dongtaiss: dongtaiss
            });
        });
    },
    listDongtaiss() {
        var me = this;
        var data = {
            offset: 0, 
            length: 25,
            topic_id: me.state.topic_id,
        };
        app.get('web/dynamic_topic/dynamicList', data, function(res) {
            var list = res.data.result.list;
            var dongtaiss = me.state.dongtaiss;
            dongtaiss.items = list;
            me.setState({
                total: res.data.result.total,
                dongtaiss: dongtaiss
            });
        });
    },
    componentWillMount () {
        if(this.init) {
            this.init();
        }
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = v;
        this.setState({n: temp[names[i]]});
    },
    handleTextareaChange(name, e) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = e.target.value;
        this.setState({n: temp[names[i]]});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidShow () {
        this.listDongtaiss();
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
